package com.hmall.serch;


import com.alibaba.fastjson.JSON;
import com.hmall.common.client.ItemClient;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.Item;
import com.hmall.serch.pojo.ItemDoc;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import org.junit.jupiter.api.Test;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@SpringBootTest
public class testdemo {

    @Autowired
    ItemClient itemClient;
    @Autowired
    RestHighLevelClient client;


    @Test
    public void listClient() throws IOException {

        Long page = 1L;
        Long size = 10000L;
        List<PageDTO> listAll = new ArrayList<>();
        BulkRequest request = new BulkRequest();
    for (int i = 0; i < 8; i++) {
        PageDTO<Item> itemPageDTO = itemClient.pageClient(page, size);
//        listAll.add(itemPageDTO);
        List<Item> list = itemPageDTO.getList();
        ItemDoc itemDoc = new ItemDoc();
        for (Item item : list) {
            BeanUtils.copyProperties(item, itemDoc);
            // 2.2.创建新增文档的Request对象
            request.add(new IndexRequest("item")
                    .id(itemDoc.getId().toString())
                    .source(JSON.toJSONString(itemDoc), XContentType.JSON));
        }
        page++;

    }

//    listAll.add(itemPageDTO);

        // 2.准备参数，添加多个新增的Request


        // 2.1.转换为文档类型HotelDoc



        // 3.发送请求
        client.bulk(request, RequestOptions.DEFAULT);
    }

    @Test
    public void test11() {
        System.out.println("haha");
        PageDTO<Item> itemPageDTO = itemClient.pageClient(1L, 5L);
        System.out.println(itemPageDTO);
    }


}
